---
title: "Scheduled_Area_Tehsils_Mandals_2011_Shrug"
author: "Pratik Mahajan"
date: "2024-06-21"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r}
library(sf)
library(dplyr)
library(ggplot2)
library(ggthemes)  
library(patchwork)
library(stringr)

```

# 
# Kindly Download the subdistrict and district 2011 polygons shape files from SHRUG: https://www.devdatalab.org/shrug_download/


```{r}
tehsils_india <-st_read("subdistrict.shp")
districts_india <- st_read("district.shp")

selected_data <- tehsils_india %>%
  select(pc11_s_id,pc11_d_id,pc11_sd_id, sd_name) %>%
  st_set_geometry(NULL) 

selected_data2 <- districts_india %>%
  select(pc11_d_id, d_name) %>%
  st_set_geometry(NULL) 
merged_data <- merge(selected_data, selected_data2, by = "pc11_d_id", all.x = TRUE)

state_names <- c("01" = "Jammu Kashmir", "02" = "Himachal Pradesh", "03" = "Punjab", "04" = "Chandigarh",
                 "05" = "Uttarakhand", "06" = "Haryana", "07" = "NCT of Delhi", "08" = "Rajasthan",
                 "09" = "Uttar Pradesh", "10" = "Bihar", "11" = "Sikkim", "12" = "Arunachal Pradesh",
                 "13" = "Nagaland", "14" = "Manipur", "15" = "Mizoram", "16" = "Tripura",
                 "17" = "Meghalaya", "18" = "Assam", "19" = "West Bengal", "20" = "Jharkhand",
                 "21" = "Odisha", "22" = "Chhattisgarh", "23" = "Madhya Pradesh", "24" = "Gujarat",
                 "25" = "Daman and Diu", "26" = "Dadra and Nagar Haveli", "27" = "Maharashtra",
                 "28" = "Andhra Pradesh", "29" = "Karnataka", "30" = "Goa", "31" = "Lakshadweep",
                 "32" = "Kerala", "33" = "Tamil Nadu", "34" = "Puducherry", "35" = "Andaman and Nicobar Islands")


# Add the s_name column to merged_data
merged_data$s_name <- state_names[merged_data$pc11_s_id]

names(merged_data) <- c("pc11_district_id", "pc11_state_id", "pc11_subdistrict_id", 
                        "subdistrict_name", "district_name", "state_name")

# Reorder columns
merged_data <- merged_data[, c("pc11_state_id", "pc11_district_id", "pc11_subdistrict_id", 
                               "state_name", "district_name", "subdistrict_name")]

write.csv(merged_data, "merged_data_empty.csv", row.names = FALSE)
```

# The Scheduled Status had to be Manually Coded verifying each annual report pdf of the states with scheduled areas which are scanned copies. 

```{r}
blocks_india <- read.csv("merged_data.csv")

# View the updated blocks_india dataset
head(blocks_india)

blocks_india <- blocks_india %>%
  mutate(scheduled_status = case_when(
    is.na(scheduled_status) ~ "Not Scheduled",
    scheduled_status == 5 ~ "Fifth Schedule",
    scheduled_status == 6 ~ "Sixth Schedule",
    TRUE ~ as.character(scheduled_status)
  ))

blocks_india <- blocks_india %>%
  mutate(latest_state_schedule_order = case_when(
    scheduled_status != "Not Scheduled" & state_name%in% c("Andhra Pradesh") ~ 1955,
    scheduled_status != "Not Scheduled" & state_name== "Himachal Pradesh" ~ 1975,
    scheduled_status != "Not Scheduled" & state_name%in% c("Gujarat", "Odisha") ~ 1977,
    scheduled_status != "Not Scheduled" & state_name== "Rajasthan" ~ 1981,
    scheduled_status != "Not Scheduled" & state_name== "Maharashtra" ~ 1985,
    scheduled_status != "Not Scheduled" & state_name%in% c("Madhya Pradesh", "Chhattisgarh") ~ 2003,
    scheduled_status != "Not Scheduled" & state_name== "Jharkhand" ~ 2007,
    scheduled_status == "Sixth Schedule" & state_name== "Tripura" ~ 1985,
    scheduled_status == "Sixth Schedule" & state_name== "Meghalaya" ~ 1972,
    scheduled_status == "Sixth Schedule" & state_name== "Mizoram" ~ 1986,
    scheduled_status == "Sixth Schedule" & state_name== "Assam" ~ 2003,
    TRUE ~ NA_real_
  ))

blocks_india <- blocks_india %>%
  mutate(pesa_first_election_in_state_year = case_when(
    scheduled_status != "Not Scheduled" & state_name%in% c("Andhra Pradesh", "TELANGANA", "Gujarat") ~ 2001,
    scheduled_status != "Not Scheduled" & state_name%in% c("Himachal Pradesh", "Madhya Pradesh", "Rajasthan") ~ 2000,
    scheduled_status != "Not Scheduled" & state_name== "Odisha" ~ 2002,
    scheduled_status != "Not Scheduled" & state_name== "Chhattisgarh" ~ 2005,
    scheduled_status != "Not Scheduled" & state_name== "Maharashtra" ~ 2007,
    scheduled_status != "Not Scheduled" & state_name== "Jharkhand" ~ 2010,
    TRUE ~ NA_real_
  ))

blocks_india <- blocks_india %>%
  mutate(pesa_rules_published_state_year = case_when(
    scheduled_status != "Not Scheduled" & state_name == "Andhra Pradesh" ~ "2011",
    scheduled_status != "Not Scheduled" & state_name == "Himachal Pradesh" ~ "2011",
    scheduled_status != "Not Scheduled" & state_name == "Rajasthan" ~ "2011",
    scheduled_status != "Not Scheduled" & state_name == "TELANGANA" ~ "2014",
    scheduled_status != "Not Scheduled" & state_name == "Maharashtra" ~ "2014",
    scheduled_status != "Not Scheduled" & state_name == "Gujarat" ~ "2017",
    scheduled_status != "Not Scheduled" & state_name == "Chhattisgarh" ~ "2022",
    scheduled_status != "Not Scheduled" & state_name == "Madhya Pradesh" ~ "2022",
    scheduled_status != "Not Scheduled" & state_name == "Odisha" ~ "Not_Published_by_2024",
    scheduled_status != "Not Scheduled" & state_name == "Jharkhand" ~ "Not_Published_by_2024",
    TRUE ~ NA_character_
  ))

write.csv(blocks_india, "scheduled_areas_by_subdistrict_india.csv", row.names = FALSE)

```

